//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EvolutionDescription.h"
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (Cephei::Core::IVector<Double>^ rateTimes, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ evolutionTimes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phEvolutionDescription = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        CDoubleVector^ _NCevolutionTimes;
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (evolutionTimes))
        {
            evolutionTimes->Value->Lock();
            INativeVector<Double>^ _NCIevolutionTimes = evolutionTimes->Value->getFeature (NativeFeature::Value);
            _NCevolutionTimes = safe_cast<CDoubleVector^>(_NCIevolutionTimes);
        }
        std::vector<QuantLib::Time>& _evolutionTimes = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>::IsSome::get (evolutionTimes) ? static_cast<std::vector<QuantLib::Time>&> (_NCevolutionTimes->GetReference ()) : std::vector<QuantLib::Time>()); //3
        _ppEvolutionDescription = new boost::shared_ptr<QuantLib::EvolutionDescription> (new QuantLib::EvolutionDescription ( _rateTimes,  _evolutionTimes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (evolutionTimes != nullptr) evolutionTimes->Value->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phEvolutionDescription = NULL;
#endif
        _ppEvolutionDescription = new boost::shared_ptr<QuantLib::EvolutionDescription> (new QuantLib::EvolutionDescription ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (boost::shared_ptr<QuantLib::EvolutionDescription>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvolutionDescription = NULL;
#endif
	_ppEvolutionDescription = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (QuantLib::EvolutionDescription& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phEvolutionDescription = NULL;
#endif
	_ppEvolutionDescription = new boost::shared_ptr<QuantLib::EvolutionDescription> (&childNative);
    
    _EvolutionDescriptionOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (CEvolutionDescription^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvolutionDescription = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEvolutionDescription = new boost::shared_ptr<QuantLib::EvolutionDescription> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvolutionDescription = NULL;
#endif
	if (!t->IsSubclassOf(CEvolutionDescription::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (QuantLib::Handle<QuantLib::EvolutionDescription>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEvolutionDescription = &childNative;
	_ppEvolutionDescription = &static_cast<boost::shared_ptr<QuantLib::EvolutionDescription>>(childNative.currentLink());
    
    _EvolutionDescriptionOwner = owner;
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (QuantLib::Handle<QuantLib::EvolutionDescription> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEvolutionDescription = &childNative;
	_ppEvolutionDescription = &static_cast<boost::shared_ptr<QuantLib::EvolutionDescription>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CEvolutionDescription::CEvolutionDescription (QuantLib::EvolutionDescription childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvolutionDescription = NULL;
#endif
	_ppEvolutionDescription = new boost::shared_ptr<QuantLib::EvolutionDescription> (new QuantLib::EvolutionDescription (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CEvolutionDescription::~CEvolutionDescription ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppEvolutionDescription != NULL)
    {
	    delete _ppEvolutionDescription;
        _ppEvolutionDescription = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CEvolutionDescription::!CEvolutionDescription ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppEvolutionDescription != NULL)
    {
	    delete _ppEvolutionDescription;
    }
}
QuantLib::EvolutionDescription& Cephei::QL::Models::Marketmodels::CEvolutionDescription::GetReference ()
{
    if (_ppEvolutionDescription == NULL) throw REFNEW NativeNullException ();
	return **_ppEvolutionDescription;
}
boost::shared_ptr<QuantLib::EvolutionDescription>& Cephei::QL::Models::Marketmodels::CEvolutionDescription::GetShared ()
{
    if (_ppEvolutionDescription == NULL) throw REFNEW NativeNullException ();
	return *_ppEvolutionDescription;
}
QuantLib::EvolutionDescription* Cephei::QL::Models::Marketmodels::CEvolutionDescription::GetPointer ()
{
    if (_ppEvolutionDescription == NULL) throw REFNEW NativeNullException ();
	return &**_ppEvolutionDescription;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EvolutionDescription>& Cephei::QL::Models::Marketmodels::CEvolutionDescription::GetHandle ()
{
	if (_phEvolutionDescription == NULL)
	{
		_phEvolutionDescription = new Handle<QuantLib::EvolutionDescription> (*_ppEvolutionDescription);
	}
	return *_phEvolutionDescription;
}
#endif
bool Cephei::QL::Models::Marketmodels::CEvolutionDescription::HasNative () 
{
	return (_ppEvolutionDescription != NULL);
}

Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CEvolutionDescription::EvolutionTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppEvolutionDescription)->evolutionTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::CEvolutionDescription::FirstAliveRate::get ()
{
    try
    {
    	std::vector<QuantLib::Size>& _rv = (std::vector<QuantLib::Size>&)(*_ppEvolutionDescription)->firstAliveRate ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CEvolutionDescription::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppEvolutionDescription)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::CEvolutionDescription::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppEvolutionDescription)->numberOfSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CEvolutionDescription::RateTaus::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppEvolutionDescription)->rateTaus ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::CEvolutionDescription::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppEvolutionDescription)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::CEvolutionDescription_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ evolutionTimes)
{
    return REFNEW CEvolutionDescription ( rateTimes,  evolutionTimes);
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::CEvolutionDescription_Factory::Create ()
{
    return REFNEW CEvolutionDescription ();
}
